package com.generator.view.controller;

import com.generator.common.constant.BaseConstant;
import com.generator.common.util.DbConnectionUtil;
import com.generator.common.util.SingletonMap;
import com.generator.view.enums.FXMLPage;
import com.generator.view.util.UiBaseUtils;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import org.apache.log4j.Logger;

/**
 * 菜单按钮事件控制器
 *
 * @author
 * @create 2018-03-17 12:55
 **/
public class MenuBtnEventController{
    private Label systemConfig;//系统设置按钮
    private Label connectionLabel;//打开连接
    private Label configsLabel;//保存配置
    private Label clearAllLabel;
    private MainUIController main;

    public MenuBtnEventController(MainUIController main,Label connectionLabel, Label systemConfig, Label configsLabel,Label clearAllLabel) {
        this.systemConfig = systemConfig;
        this.connectionLabel = connectionLabel;
        this.configsLabel = configsLabel;
        this.clearAllLabel=clearAllLabel;
        this.main = main;
    }

    public void init(){
        UiBaseUtils.setMenuButtonIcon(systemConfig, BaseConstant.SYSTEM_ICON);
        UiBaseUtils.setMenuButtonIcon(connectionLabel,BaseConstant.COMPUTER_ICON);
        UiBaseUtils.setMenuButtonIcon(configsLabel,BaseConstant.CONFIG_ICON);
        UiBaseUtils.setMenuButtonIcon(clearAllLabel,BaseConstant.DEL_ICON);
        startListening();
    }

    public void startListening(){
        systemConfig.setOnMouseClicked(event -> {
            MouseButton button = event.getButton();
            if(button != MouseButton.PRIMARY)return;
            UiBaseUtils.openDialog(SystemConfigController.class,systemConfig.getText(), FXMLPage.SYSTEM_CONFIG, true);

        });

        connectionLabel.setOnMouseClicked(event -> {
            MouseButton button = event.getButton();
            if(button != MouseButton.PRIMARY)return;
            DbConnectionController controller = (DbConnectionController)UiBaseUtils.openDialog(DbConnectionController.class,connectionLabel.getText(), FXMLPage.NEW_CONNECTION, true);
            controller.setMainUIController(main);
        });

        configsLabel.setOnMouseClicked(event -> {
            MouseButton button = event.getButton();
            if(button != MouseButton.PRIMARY)return;
            GeneratorConfigController controller = (GeneratorConfigController)UiBaseUtils.openDialog(GeneratorConfigController.class,configsLabel.getText(),FXMLPage.GENERATOR_CONFIG,true);
            controller.setMainUIController(main);
        });

        clearAllLabel.setOnMouseClicked(event -> {
            MouseButton button = event.getButton();
            if(button != MouseButton.PRIMARY){
                return;
            }
//            SingletonMap.getInstance().putValue("kkk","22");
//            System.out.print("ss:"+DbConnectionUtil.getSqliteDao().getSystemConfig(SingletonMap.getInstance().getValueByKey("username")));
//            DbConnectionUtil.getSqliteDao().deleteConfig();
//            System.out.print("ss:"+DbConnectionUtil.getSqliteDao().selectSystemConfig());
//            LOG.info("==============:kkk:"+SingletonMap.getInstance().getValueByKey("kkk"));

//            GeneratorConfigController controller = (GeneratorConfigController)UiBaseUtils.openDialog(GeneratorConfigController.class,configsLabel.getText(),FXMLPage.GENERATOR_CONFIG,true);
//            controller.setMainUIController(main);
        });
    }
    private static final Logger LOG = Logger.getLogger(MainUIController.class);

}
